package phonecompany.call;

import java.io.Serializable;
import java.util.Date;

import phonecompany.PhoneManager;
import phonecompany.account.Account;
import phonecompany.account.PayAsYouGoAccount;
import phonecompany.account.UnlimitedCallsAccount;
import phonecompany.phone.PhoneNumber;

/**
 * This class define a call entity which contain infromation about the from
 * phone number , to phone number started time ,duration ,cost and current
 * account.
 * 
 * @since Oct 25, 2011
 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
 * @version $Id: Call.java 28 2011-10-30 21:25:58Z zexpp5@gmail.com $
 * 
 */
public class Call implements Serializable {
	private PhoneNumber fromNumber;
	private Account acc;
	private PhoneNumber toNumber;
	private Date startTime;
	private int duration = -1;// minute
	private int cost = -1;// pence

	private Call() {
	}

	/**
	 * A Call could only be generated by this method with fromNumber,toNumber
	 * and duration.
	 * 
	 * Constructor Method
	 * 
	 * @param fromNumber
	 * @param toNumber
	 * @param duration
	 * @throws Exception
	 *             an exception will occur when current account has not enough
	 *             balance.
	 */
	public Call(PhoneNumber fromNumber, PhoneNumber toNumber, int duration)
			throws Exception {
		this.fromNumber = fromNumber;
		this.acc = PhoneManager.getPhoneManager().getAccount(fromNumber);
		this.toNumber = toNumber;
		this.startTime = new Date();
		this.duration = duration;
		if (acc instanceof PayAsYouGoAccount && acc.getBalance() * 100 < 20) {
			duration = -1;
			throw new Exception("not enough balance!");
		}

	}

	/**
	 * To do the calculation of cost for current call;
	 * 
	 * @return cost by pense
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:10:30 PM
	 */
	private int costCalculator() {

		cost = acc.getTariff() * duration;
		return cost;
	}

	/**
	 * To get the duration of current call
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:11:17 PM
	 */
	public int getDuration() {
		return duration;
	}

	/**
	 * To set the duration of current call.
	 * 
	 * @param duration
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:11:35 PM
	 */
	public void setDuration(int duration) {
		this.duration = duration;
	}

	/**
	 * To get the from PhoneNumber of current call
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:11:54 PM
	 */
	public PhoneNumber getFromNumber() {
		return fromNumber;
	}

	/**
	 * To get the to PhoneNumber of current call
	 * 
	 * @return
	 * @description
	 * 
	 * @author <a href="mailto:zexpp5@gmail.com">Zexu.Lin</a>
	 * @update Oct 30, 2011 7:12:31 PM
	 */
	public PhoneNumber getToNumber() {
		return toNumber;
	}

}
